﻿unit MVC.WINServer;

interface

uses

  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.SvcMgr, Vcl.Dialogs,mvc.JSON,mvc.Config,mvc.LogUnit;

type
  TMServiceType = Vcl.SvcMgr.TServiceType;

  TMStartType = Vcl.SvcMgr.TStartType;

  TWINService = class(TService)
    procedure ServiceStart(Sender: TService; var Started: Boolean);
    procedure ServiceStop(Sender: TService; var Stopped: Boolean);
  private    { Private declarations }
  public
    function GetServiceController: TServiceController; override;
    procedure setParam(sName, sDisplayName: string; sServiceType: TMServiceType; sStartType: TMStartType);
    { Public declarations }
  end;
  TMVCWinService = class
  private
  public
    FSName: string;
    FDisplayName: string;
    FServiceType: TMServiceType;
    FStartType: TMStartType;
    procedure Init;
  end;

var
  WINService: TWINService;


implementation

{$R *.dfm}
uses
  MVC.App;

procedure ServiceController(CtrlCode: DWord); stdcall;
begin
  WINService.Controller(CtrlCode);

end;

function TWINService.GetServiceController: TServiceController;
begin
  Result := ServiceController;
end;

procedure TWINService.ServiceStart(Sender: TService; var Started: Boolean);
begin

  StartServer();
  log('win服务启动');
  Started := true;
end;

procedure TWINService.ServiceStop(Sender: TService; var Stopped: Boolean);
begin

  CloseServer();
  Stopped := true;
end;

procedure TWINService.setParam(sName, sDisplayName: string; sServiceType: TMServiceType; sStartType: TMStartType);
begin
  Name := sName;
//  Dependencies
  DisplayName := sDisplayName;
  ServiceType := sServiceType;
  StartType := sStartType;
end;
{ TWinService }

procedure TMVCWinService.Init;
var
  ServiceT, StartT: string;
  cof: IJObject;
begin

  if Config.WinServiceConfig <> '' then
  begin
    cof := IIjobject(Config.WinServiceConfig);
    FSName := cof.GetS('Name');
    FDisplayName := cof.GetS('DisplayName');
    ServiceT := cof.GetS('ServiceType');
    StartT := cof.GetS('StartType');

    if ServiceT = 'stWin32' then
      FServiceType := TMServiceType.stWin32;
    if ServiceT = 'stDevice' then
      FServiceType := TMServiceType.stDevice;
    if ServiceT = 'stFileSystem' then
      FServiceType := TMServiceType.stFileSystem;

    if StartT = 'stBoot' then
      FStartType := TStartType.stBoot;
    if StartT = 'stSystem' then
      FStartType := TStartType.stSystem;
    if StartT = 'stAuto' then
      FStartType := TStartType.stAuto;
    if StartT = 'stManual' then
      FStartType := TStartType.stManual;
  end;
end;
end.

